home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18040 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: 'chained': It would be nice if...
  5. Date: Thu, 18 Apr 1996 16:47:38 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4l5rlt$dkp@news.halcyon.com>
  8. References: <31756DEC.5215@zurich.ibm.com> <317505E6.7F7F@cs.tu-berlin.de>
  9. NNTP-Posting-Host: blv-pm3-ip12.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Roman Lechtchinsky <wolfro@cs.tu-berlin.de> wrote:
  13.  
  14. >Keith Whittingham wrote:
  15. >> 
  16. >> Three or four times I've find myself needing a construct which I
  17. >> think is missing from C++ (or at least is inaccessable).
  18. >> 
  19. >> A keyword, say 'chained', to have a similar effect to that of
  20. >> the virtual destructor. e.g.
  21. >> 
  22. >> class Base
  23. >>   {
  24. >>   public:
  25. >>     virtual void vMember(void);
  26. >>     chained void cMember(void);
  27. >>   };
  28. >> 
  29. >> class Derived:
  30. >>   public Base
  31. >>   {
  32. >>   public:
  33. >>     void vMember(void);
  34. >>     void cMember(void);
  35. >>   };
  36. >> 
  37. >> Calling Derived::vMember() executes the code contained in the body
  38. >> Derived::vMember() whereas calling Derived::cMember() would
  39. >> execute the code contained in the body Base::cMember() and then
  40. >> the code contained in Derived::cMember().
  41.  
  42. >How about:
  43.  
  44. >class Base
  45. >{
  46. >public:
  47. > void    cMember(void)
  48. >    {
  49. >     Base_cMember();
  50. >     Derived_cMember();
  51. >    }
  52. >protected:
  53. > virtual void    Derived_cMember();
  54. >private:
  55. > void    Base_cMember();
  56. >};
  57.  
  58. >Every time cMember is called the unique private code in Base_cMember is 
  59. >executed. Then Derived_cMember is called which can be overridden in derived 
  60. >classes.
  61.  
  62. >Bye
  63.  
  64. >Roman
  65.  
  66. This is fine when the hierarchy is two classes deep.  But
  67. Derived_Derived overrides Derived_cMember() and now cMember calls
  68. Base_cMember() then Derived_Derived::Derived_cMember.  The
  69. Derived::Devied_cMember is skipped.   
  70.  
  71. The proposed "chained" should probably have two forms: call-before and
  72. call-after.  While it does remove the possibility someone will forget
  73. to call the base-class implementation, it also ties their hands as to
  74. whether they do some pre-processing or post-processing, try-catch
  75. blocks, etc.  It also may have problems in multiple, virtual
  76. inheritence hierarchies; if D derives from B and C which both derive
  77. from A, how is the chain supposed to travel?
  78.  
  79. I think you're only recourse is to publish in the header "You'd be a
  80. fool to not call the base-class implementation."
  81.  
  82. I can sympathize, however; I've wanted to force class designers to
  83. call my base methods, too.  
  84.  
  85.                         --Norm 
  86.  
  87.